﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Data.SqlClient;

namespace Xmhouse.Passport
{
    /// <summary>
    /// 缓存管理
    /// 将用户凭证、令牌的关系数据存放于Cache中
    /// </summary>
    public class CacheManager
    {
        private Xmhouse.Data.SQLConn objConn = new Xmhouse.Data.SQLConn("PassportConn");
        public string ErrInfo = ""; 

        /// <summary>
        /// 根据令牌获取用户凭证
        /// </summary>
        /// <param name="tokenValue">令牌</param>
        /// <returns></returns>
        public CacheManager()
        {


            
        }
        public string GetInfo(string tokenValue)
        {
            string ReturnValue = string.Empty;

            SqlParameter[] SqlParas = new SqlParameter[4];
            SqlParas[0] = new SqlParameter("@token", tokenValue);
            SqlParas[1] = new SqlParameter("@info",SqlDbType.NVarChar ,2000);
            SqlParas[1].Direction = ParameterDirection.Output;

            if (objConn.PrepareCommand("[sp_TokenGetInfo]", SqlParas) == 1)
            {
                ReturnValue = SqlParas[1].Value.ToString();
            }
            else
            {
                ErrInfo = objConn.ErrInfo;
                ReturnValue =  "";
            }
            return ReturnValue;
        }

        /// <summary>
        /// 清除令牌凭证
        /// </summary>
        /// <param name="tokenValue">令牌</param>
        /// <returns></returns>

        public bool Clear(string tokenValue)
        {
            bool ReturnValue = false;

            SqlParameter[] SqlParas = new SqlParameter[4];
            SqlParas[0] = new SqlParameter("@token", tokenValue);

            if (objConn.PrepareCommand("[sp_TokenDelete]", SqlParas) == 1)
            {
                ReturnValue = true;
            }
            else
            {
                ErrInfo = objConn.ErrInfo;
                ReturnValue = false;
            }
            return ReturnValue;
        }


        /// <summary>
        /// 添加令牌
        /// </summary>
        /// <param name="token">令牌</param>
        /// <param name="info">凭证</param>
        /// <param name="timeout">过期时间</param>
        public  bool   TokenInsert(string token, string info, int timeout,string url,string ip,string username,int userid,string initmodel)
        {
            SqlParameter[] SqlParas = new SqlParameter[10];

            SqlParas[0] = new SqlParameter("@token", token);
            SqlParas[1] = new SqlParameter("@info", info);
            SqlParas[2] = new SqlParameter("@timeout", timeout);
            SqlParas[3] = new SqlParameter("@loginurl", url);
            SqlParas[4] = new SqlParameter("@ip", ip);
            SqlParas[5] = new SqlParameter("@username", username);
            SqlParas[6] = new SqlParameter("@publicid", userid);
            SqlParas[7] = new SqlParameter("@InitializeModel", initmodel);

            if (objConn.PrepareCommand("sp_TokenInsert", SqlParas) == 1)
            {
                return true; 
            }
            else
            {
                ErrInfo = objConn.ErrInfo;
                return false;
            }
        }

        public void Dispose()
        {
            objConn = null;
        }

    }//end class
}
